package com.mjh.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.mjh.bean.Article;
import com.mjh.comm.Constant;
import com.mjh.comm.page.Pager;
import com.mjh.config.GlobalConfig;
import com.mjh.dao.ArticleDAO;

public class ArticleManager {

	public static Logger log = Logger.getLogger(ArticleManager.class);
	
	private ArticleDAO dao = new ArticleDAO();
	
	public int addArticle(Article article){
		return dao.insert(article);
	}
	
	public int updateArticle(Article article){
		return dao.update(article);
	}
	/**
	 * 如果该文章已经创建过索引，则将索引标志设为旧，要更新索引
	 * @param articleId
	 * @return
	 */
	public int updateaArticleIndexStatus(int articleId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("indexStatus", Constant.ARTICLE_INDEX_OLD);
		parameter.put("indexBeforeStatus", Constant.ARTICLE_IS_INDEX);
		parameter.put("ids", new Integer[]{articleId});
		return dao.updateIndexStatus(parameter);
	}
	
	public int delArticle(int id){
		return dao.delete(id);
	}
	
	public Article getArticle(int id){
		return dao.selectById(id);
	}
	
	public List<Article> listByCategory(int category, int limitNum){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("category", category);
		parameter.put("status", Constant.ARTICLE_STATE_NORMAL);
		parameter.put("pageStart", 0);
		parameter.put("pageSize", limitNum);
		return dao.selectForList(parameter);
	}

	public List<Article> listTopClick(int top){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("top", top);
		parameter.put("type", "all");
		parameter.put("status", Constant.ARTICLE_STATE_NORMAL);
		return dao.selectTopClick(parameter);
	}
	
	public List<Article> listTopClickLastWeek(int top){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("top", top);
		parameter.put("type", "lastWeek");
		parameter.put("status", Constant.ARTICLE_STATE_NORMAL);
		return dao.selectTopClick(parameter);
	}
	
	public Pager<Article> listAll(int pagerNo, Map<String,Object> para){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		Pager<Article> pager = new Pager<Article>();
		pager.setPageNo(pagerNo);
		pager.setPageSize(GlobalConfig.getSysConfig().getArticleListPageSize());
		parameter.put("pageStart", pager.getStartNo());
		parameter.put("pageSize", pager.getPageSize());
		parameter.putAll(para);
		pager.setTotalNum(dao.selectCount(parameter));
		pager.setPageData(dao.selectForList(parameter));
		return pager;
	}
	
	public int getTotalRecordNum(Map<String,Object> para){
		return dao.selectCount(para);
	}	
	
	public void clickArticle(int articleId){
		dao.recordClickNumber(articleId);
	}
	
	public static void main(String[] args) {
		ArticleManager m = new ArticleManager();
		m.listTopClick(10);
	}
}
